<?php
Globals::requireClass('Controller');
Globals::requireTable('User');
Globals::requireTable('Groupacl');
class LoginController extends Controller
{
	protected $UserTable;
	protected $GroupaclTable;
        public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->UserTable 	= new UserTable($config);
                $this->GroupaclTable = new GroupaclTable($config);
	}
	
	public function LoginAction(){
	 //$this->config['layoutEnabled'] = false;
	}
        
    public function dologinAction(){
        $username = $this->getParam('username');
        $password = md5($this->getParam('password'));
       // echo $password;exit;
        $data =  $this->UserTable->getRow("`username` = '{$username}' AND `password` = '{$password}' AND `status` = 1");
       // var_dump($data);exit;
             if($data){
               //  var_dump($data);exit;
                 $_SESSION['userinfo']['username'] = $data['username'];
                 $_SESSION['userinfo']['id'] = $data['id'];
                 $group_id = $data['group_id'];
                 if(!empty($group_id)){
                     $result = $this->GroupaclTable->listAll("`group_id` = $group_id");
                 }
                 $result = @i_array_column($result,'acl_id');
                 $_SESSION['userinfo']['group_id'] = $group_id;
                 $_SESSION['userinfo']['acl'] = $result;
                 
                 $fp = fopen('./sessionid/'.$data['id'].'.txt', 'w+');
                 fwrite($fp, session_id());
                 fclose($fp);
                 
                 echo "sucess";
             }else{
                 echo "error";
             }
             
             exit;
        }
        
        public function logoutAction(){
            $uid = $_SESSION['userinfo']['id'];
                unset($_SESSION['userinfo']);
                unlink('./sessionid/'.$uid.'.txt');
                $this->redirect("./?m=login&a=login");
            
//            unset($_SESSION['userinfo']);
//           $this->redirect("./?m=login&a=login");
           exit;
        }
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('LoginController', 'Controller');
